#!/bin/sh
echo $0 $*
XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share}

# 修改为直接获取文件夹名称的用法,不同游戏文件名不同
GAMEDIR="/mnt/SDCARD/Ports/fheroes2"
# GAMEDIR=${PWD}
echo ==========GAMEDIR is $GAMEDIR
controlfolder="../PortMaster"

source $controlfolder/control.txt
#source $controlfolder/tasksetter

get_controls

# 增加错误输出记录
exec > >(tee "$GAMEDIR/log.txt") 2>&1

CONFIGFOLDER="/mnt/SDCARD/Ports/fheroes2"
cd "${CONFIGFOLDER}"

$ESUDO chmod 666 /dev/tty0
$ESUDO rm -rf ~/.config/fheroes2
ln -sfv ${CONFIGFOLDER}/conf/fheroes2/ ~/.config/
$ESUDO rm -rf ~/.local/share/fheroes2
ln -sfv ${CONFIGFOLDER}/save/fheroes2/ ~/.local/share/

$ESUDO chmod 666 /dev/tty1
$ESUDO chmod 666 /dev/uinput
export LD_LIBRARY_PATH="$CONFIGFOLDER/libs:$LD_LIBRARY_PATH"

$GPTOKEYB "fheroes2" &
SDL_GAMECONTROLLERCONFIG="$sdl_controllerconfig" ./fheroes2 2>&1 | tee $CONFIGFOLDER/log.txt
$ESUDO kill -9 $(pidof gptokeyb)
$ESUDO systemctl restart oga_events &
printf "\033c" >> /dev/tty1
printf "\033c" > /dev/tty10